package com.example.nfcaccesssystem;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListView;

public class EditDoorActivity extends Activity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.activity_edit_door);
	
	    Intent i = getIntent();
	    
	    final Door d = (Door) i.getSerializableExtra("door");
	    
	    ListView userAccessListView = (ListView)findViewById(R.id.userAccessListView);
	    
	    ArrayAdapter<User> adapter = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_multiple_choice, Controller.getInstance().userList);
	    
		userAccessListView.setAdapter(adapter);
	    
	    Controller.getInstance().getUsersAndAccess(d, this);
	    
	    EditText doorInfoText = (EditText)findViewById(R.id.doorInfoText);
	    
	    doorInfoText.setText(d.getDoorInfo());
	    
	    userAccessListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    
	    userAccessListView.setOnItemClickListener(new OnItemClickListener() {
	    	
	    	@Override
	    	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg){
	    		User u = (User) adapter.getItemAtPosition(position);
	    		
	    		CheckedTextView checkedView = (CheckedTextView)view;
	    		
	    		boolean checked = checkedView.isChecked();
	    		
	    		if(checked){
	    			
	    			Controller.getInstance().addUserDoorAccess(u, d);
	    		}

	    		else {
	    				
	    			Controller.getInstance().removeUserDoorAccess(u, d);
	    		}
	    	}
		});
	    
	    Button okButton = (Button)findViewById(R.id.editDoorInfoOkButton);
	    
	    okButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText doorInfoText = (EditText)findViewById(R.id.doorInfoText);
				
				Controller.getInstance().changeDoorInfo(d.getId(), doorInfoText.getText().toString());
			}
		});
	    
	    Button deleteButton = (Button)findViewById(R.id.deleteDoorButton);
	    
	    deleteButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Controller.getInstance().deleteDoor(d.getId());
			}
		});
	}
	
	public void backEditDoor(View v) {
		
	}
	
	public void setUsersDoorAccess(boolean success) {
		if(success){
			
			//Show a notification if successful or unsuccessful.
		}
		else {
			
		}
	}
	
	public void setUsersAndAccess() {
		
		ListView userAccessListView = (ListView)findViewById(R.id.userAccessListView);
		
		ArrayAdapter<User> adap = (ArrayAdapter<User>)userAccessListView.getAdapter();
		
		adap.clear();
		
		HashMap<User, Boolean> map = Controller.getInstance().doorAccessMap;
		
		for(User u : Controller.getInstance().doorAccessMap.keySet()) {
			adap.add(u);
		}
		
		int count = adap.getCount();
		
	    for (int j = 0; j < count; j++){
	        	
	    	if (Controller.getInstance().doorAccessMap.get(Controller.getInstance().userList.get(j)) == true) {
			    userAccessListView.setItemChecked(j, true);
			}
	    }
	    
	    adap.notifyDataSetChanged();
	}
	
	public void setDeleteNewDoor(boolean response) {
		if (response) {
			Intent i = new Intent(this, MainActivity.class);
			startActivity(i);
		}
	}
}
